package cn.richx.pushmessage.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.richx.pushmessage.domain.PushMessage;
import cn.richx.pushmessage.service.PushMessageService;
import cn.richx.pushmessage.web.rest.errors.BadRequestAlertException;
import cn.richx.pushmessage.web.rest.util.HeaderUtil;
import cn.richx.pushmessage.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing PushMessage.
 */
@RestController
@RequestMapping("/api")
public class PushMessageResource {

    private final Logger log = LoggerFactory.getLogger(PushMessageResource.class);

    private static final String ENTITY_NAME = "pushMessage";

    private final PushMessageService pushMessageService;

    public PushMessageResource(PushMessageService pushMessageService) {
        this.pushMessageService = pushMessageService;
    }

    /**
     * POST  /push-messages : Create a new pushMessage.
     *
     * @param pushMessage the pushMessage to create
     * @return the ResponseEntity with status 201 (Created) and with body the new pushMessage, or with status 400 (Bad Request) if the pushMessage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/push-messages")
    @Timed
    public ResponseEntity<PushMessage> createPushMessage(@RequestBody PushMessage pushMessage) throws URISyntaxException {
        log.debug("REST request to save PushMessage : {}", pushMessage);
        if (pushMessage.getPushId() != null) {
            throw new BadRequestAlertException("A new pushMessage cannot already have an ID", ENTITY_NAME, "idexists");
        }
        PushMessage result = pushMessageService.save(pushMessage);
        return ResponseEntity.created(new URI("/api/push-messages/" + result.getPushId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getPushId().toString()))
            .body(result);
    }

    /**
     * PUT  /push-messages : Updates an existing pushMessage.
     *
     * @param pushMessage the pushMessage to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated pushMessage,
     * or with status 400 (Bad Request) if the pushMessage is not valid,
     * or with status 500 (Internal Server Error) if the pushMessage couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/push-messages")
    @Timed
    public ResponseEntity<PushMessage> updatePushMessage(@RequestBody PushMessage pushMessage) throws URISyntaxException {
        log.debug("REST request to update PushMessage : {}", pushMessage);
        if (pushMessage.getPushId() == null) {
            return createPushMessage(pushMessage);
        }
        PushMessage result = pushMessageService.save(pushMessage);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, pushMessage.getPushId().toString()))
            .body(result);
    }

    /**
     * GET  /push-messages : get all the pushMessages.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of pushMessages in body
     */
    @GetMapping("/push-messages")
    @Timed
    public ResponseEntity<List<PushMessage>> getAllPushMessages(Pageable pageable) {
        log.debug("REST request to get a page of PushMessages");
        Page<PushMessage> page = pushMessageService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/push-messages");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /push-messages/:id : get the "id" pushMessage.
     *
     * @param id the id of the pushMessage to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the pushMessage, or with status 404 (Not Found)
     */
    @GetMapping("/push-messages/{id}")
    @Timed
    public ResponseEntity<PushMessage> getPushMessage(@PathVariable Long id) {
        log.debug("REST request to get PushMessage : {}", id);
        PushMessage pushMessage = pushMessageService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(pushMessage));
    }

    /**
     * DELETE  /push-messages/:id : delete the "id" pushMessage.
     *
     * @param id the id of the pushMessage to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/push-messages/{id}")
    @Timed
    public ResponseEntity<Void> deletePushMessage(@PathVariable Long id) {
        log.debug("REST request to delete PushMessage : {}", id);
        pushMessageService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
